package com.ysu.zyw.common.util;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

/**
 * ValidateUtil provide an set of operation for validate.
 * <p>
 * Created by ZeRur on 2016/1/17.
 *
 * @author yaowu.zhang
 */
public class ValidateUtil {

    // inner regexp
    public static final String SPECIAL_CHAR_REGEXP = "[`~!@#$%^&*()+=|{}':;',//[//].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";

    public static final String PHONE_NUMBER_REGEXP = "^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";

    public static final String E_MAIL_REGEXP = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";

    /**
     * contains spec char in str.
     *
     * @param text str
     * @return if contains return true else return false
     */
    public static boolean containsSpecialChar(String text) {
        Assert.hasText(text, "empty text is not allowed");
        if (StringUtils.hasText(text.trim())) {
            return false;
        }

        if (!text.trim().equals(text)) {
            return false;
        }

        Pattern pattern = Pattern.compile(SPECIAL_CHAR_REGEXP);
        return pattern.matcher(text).find();
    }

    /**
     * the given str is a time.
     *
     * @param text   text
     * @param format format, if null use default format
     * @return if is a time return true else return false
     */
    public static boolean isDate(String text, String format) {
        Assert.hasText(text, "empty text is not allowed");
        try {
            new SimpleDateFormat(StringUtils.hasText(format.trim()) ? "yyyy-MM-dd HH:mm:ss" : format).parse(text);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * is integer.
     *
     * @param text text
     * @return if is integer return true else return false
     */
    public static boolean isInteger(String text) {
        Assert.hasText(text, "empty text is not allowed");
        try {
            //noinspection ResultOfMethodCallIgnored
            Integer.parseInt(text);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * is float
     *
     * @param text text
     * @return if is float return true else return false
     */
    public static boolean isFloat(String text) {
        Assert.hasText(text, "empty text is not allowed");
        try {
            return Double.parseDouble(text) != (int) Double.parseDouble(text)
                    || text.split("\\.")[1].split("0").length == 0;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * is phone num
     *
     * @param text text
     * @return if is phone num return true else return fase
     */
    public static boolean isPhoneNumber(String text) {
        Assert.hasText(text, "empty text is not allowed");
        return text.matches(PHONE_NUMBER_REGEXP);
    }

    /**
     * is email
     *
     * @param text text
     * @return if is email return true else return fase
     */
    public static boolean isEmail(String text) {
        Assert.hasText(text, "empty text is not allowed");
        return text.matches(E_MAIL_REGEXP);
    }

    /**
     * is phone num
     *
     * @param ch ch
     * @return if is chinese return true else return fase
     */
    public static boolean isChinese(Character ch) {
        Assert.notNull(ch, "null ch is not allowed");
        return new String(new char[]{ch}).matches("[\u4E00-\u9FA5]");
    }

}
